package opensource.xinye.com.test11

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_client_udp.*
import opensource.xinye.com.test11.jni.Socket

class ClientUdpActivity : Activity() {
    companion object {
        fun launch(activity: Activity){
            activity.startActivity(Intent(activity,ClientUdpActivity::class.java))
        }
    }

    private lateinit var mSocketHelper: SocketHelper
    private lateinit var mSocket: Socket

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_client_udp)

        mSocket = Socket({
            mSocketHelper.logMessage(it)
        })

        mSocketHelper = SocketHelper(this,{
            val port = mSocketHelper.getPort()
            val message = message.text.toString().trim()
            val ip = ip.text.toString().trim()
            startClient(ip,port,message)
        })
    }

    private fun startClient(ip: String, port: Int, message: String) {
        mSocketHelper.Task("ClientTask",{
                //
            },{
                mSocketHelper.logMessage("Client Starting...")
                // 执行中 -- 后台线程
                mSocket.nativeStartUdpClient(ip,port,message)

                mSocketHelper.logMessage("Client Terminated")
            },{

            }).start()
    }
}